home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------
-
- AOCE Post Office Protocol (POP) / Simple Mail Transfer Protocol (SMTP)
- Mail Service Access Module
-
- written by Steve Falkenburg-- MacDTS
- ©1991-1993 Apple Computer, Inc.
-
- --------------
- change history
- --------------
-
- SJF 02/19/93 update for beta build b1
- SJF 10/29/92 update to a11 a11
- SJF 06/08/92 update to a8 a8
- SJF 02/15/92 first working version a4.5
- SJF 10/16/91 initial coding a3
-
- ---------------------------------------------------------------------*/
-
- /* this is a generic set of routines to maintain a temporary
- spool area to spool data and info blocks for incoming and
- outgoing messages
- */
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __FOLDERS__
- #include <Folders.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- #ifndef __FILES__
- #include <Files.h>
- #endif
-
- #ifdef applec
- #include <SysEqu.h>
- #endif
-
- #include "const.h"
- #include "gwerrors.h"
- #include "globals.h"
- #include "mytypes.h"
- #include "utils.h"
- #include "spoolsystem.h"
-
-
- /* fsspec for root folder */
-
- static FSSpec gRootSpool;
-
-
- /* initialize spool stuff */
-
- OSErr InitSpoolSubsystem(void)
- {
- OSErr err;
- long dirID;
- short vRefNum;
-
- err = FindFolder(kOnSystemDisk,kTemporaryFolderType,true,&vRefNum,&dirID);
-
- if (err==noErr) {
- pstrcpy(gRootSpool.name,kSpoolFolderName);
- gRootSpool.parID = dirID;
- gRootSpool.vRefNum = vRefNum;
- err = FSpDirCreate(&gRootSpool,smSystemScript,&dirID);
- if (err==dupFNErr) {
- err = noErr;
- RecursiveDeleteDirectory(gRootSpool.vRefNum,gRootSpool.parID,gRootSpool.name);
- err = FSpDirCreate(&gRootSpool,smSystemScript,&dirID);
- }
- }
-
- return err;
- }
-
-
- /* release spool subsystem by clearing spool database */
-
- OSErr ExitSpoolSubsystem(void)
- {
- OSErr err;
-
- err = RecursiveDeleteDirectory(gRootSpool.vRefNum,gRootSpool.parID,gRootSpool.name);
- return err;
- }
-
-
- /* create a spool area on disk, and return it's FSSpec */
-
- OSErr CreateSpoolFile(FSSpec *sFile)
- {
- OSErr err;
- long randNum;
- Str255 randStr;
- long dirID;
-
- #ifdef __SYSEQU__
- BlockMove((Ptr)TimeLM,(Ptr)RndSeed,sizeof(long));
- #else
- RndSeed = Time;
- #endif
-
- randNum = (unsigned long) Random();
- NumToString(randNum,randStr);
-
- err = GetFSSpecDirID(&gRootSpool,&dirID);
- if (err!=noErr) {
- return err;
- }
-
- sFile->vRefNum = gRootSpool.vRefNum;
- sFile->parID = dirID;
- pstrcpy(sFile->name,"\pspool");
- pstrcat(sFile->name,randStr);
-
- err = FSpDirCreate(sFile,smSystemScript,&dirID);
-
- return err;
- }
-
-
- /* remove a spool area from the disk, including any spool information contained within */
-
- OSErr RemoveSpoolFile(FSSpec *sFile)
- {
- OSErr err;
-
- err = RecursiveDeleteDirectory(sFile->vRefNum,sFile->parID,sFile->name);
- return err;
- }
-
-
- /* spool a data block to the file, given a type, creator, ID, and the data with its length */
-
- OSErr SpoolToFile(FSSpec *sFile,OSType type,OSType creator,long id,char *data,unsigned long dataLen)
- {
- OSErr err;
- FSSpec newSpec;
- Str255 idStr;
- short fRefNum;
- long dirID;
- char osTypeStr[5];
-
- NumToString(id,idStr);
-
- /* get dirID of parent */
-
- err = GetFSSpecDirID(sFile,&dirID);
- if (err!=noErr) {
- return err;
- }
-
- /* make new spool datafile */
-
- newSpec.parID = dirID;
- newSpec.vRefNum = sFile->vRefNum;
- pstrcpy(newSpec.name,"\pdata");
- pstrcat(newSpec.name,idStr);
- osTypeStr[0] = 4;
- BlockMove(&creator,&osTypeStr[1],4);
- pstrcat(newSpec.name,osTypeStr);
- BlockMove(&type,&osTypeStr[1],4);
- pstrcat(newSpec.name,osTypeStr);
-
- err = FSpCreate(&newSpec,creator,type,smSystemScript);
- if (err!=noErr) {
- return err;
- }
-
- /* write data out to file */
-
- err = FSpOpenDF(&newSpec,fsRdWrPerm,&fRefNum);
- if (err!=noErr) {
- return err;
- }
- err = FSWrite(fRefNum,(long *)&dataLen,data);
- if (err!=noErr) {
- FSClose(fRefNum);
- return err;
- }
- err = FSClose(fRefNum);
-
- return err;
- }
-
-
- /* append additional info to a spool element */
-
- OSErr AppendToSpool(FSSpec *sFile,OSType type,OSType creator,long id,char *data,unsigned long dataLen)
- {
- OSErr err;
- FSSpec newSpec;
- Str255 idStr;
- short fRefNum;
- long dirID;
- char osTypeStr[5];
-
- NumToString(id,idStr);
-
- /* get dirID of parent */
-
- err = GetFSSpecDirID(sFile,&dirID);
- if (err!=noErr) {
- return err;
- }
-
- /* append data */
-
- newSpec.parID = dirID;
- newSpec.vRefNum = sFile->vRefNum;
- pstrcpy(newSpec.name,"\pdata");
- pstrcat(newSpec.name,idStr);
- osTypeStr[0] = 4;
- BlockMove(&creator,&osTypeStr[1],4);
- pstrcat(newSpec.name,osTypeStr);
- BlockMove(&type,&osTypeStr[1],4);
- pstrcat(newSpec.name,osTypeStr);
-
- err = FSpOpenDF(&newSpec,fsRdWrPerm,&fRefNum);
- if (err!=noErr) {
- return err;
- }
-
- /* go to end of file */
-
- err = SetFPos(fRefNum,fsFromLEOF,0);
- if (err!=noErr) {
- FSClose(fRefNum);
- return err;
- }
-
- err = FSWrite(fRefNum,(long *)&dataLen,data);
- if (err!=noErr) {
- FSClose(fRefNum);
- return err;
- }
- err = FSClose(fRefNum);
-
- return err;
- }
-
-
- /* get info out of a spool element (max bytes *dataLen, returned bytes in
- *dataLen, startOffset is offset from start of element */
-
- OSErr GetFromSpool(FSSpec *sFile,OSType type,OSType creator,long id,char *data,
- unsigned long *dataLen,long startOffset)
- {
- OSErr err;
- FSSpec newSpec;
- Str255 idStr;
- short fRefNum;
- long dirID;
- char osTypeStr[5];
- long testGet;
- char testData[1];
-
- NumToString(id,idStr);
-
- /* get dirID of parent */
-
- err = GetFSSpecDirID(sFile,&dirID);
- if (err!=noErr) {
- return err;
- }
-
- /* read data */
-
- newSpec.parID = dirID;
- newSpec.vRefNum = sFile->vRefNum;
- pstrcpy(newSpec.name,"\pdata");
- pstrcat(newSpec.name,idStr);
- osTypeStr[0] = 4;
- BlockMove(&creator,&osTypeStr[1],4);
- pstrcat(newSpec.name,osTypeStr);
- BlockMove(&type,&osTypeStr[1],4);
- pstrcat(newSpec.name,osTypeStr);
-
- err = FSpOpenDF(&newSpec,fsRdWrPerm,&fRefNum);
- if (err!=noErr) {
- return kNoData;
- }
-
- /* go to spot in file */
-
- err = SetFPos(fRefNum,fsFromStart,startOffset);
- if (err!=noErr) {
- return err;
- }
-
- /* read bytes */
-
- err = FSRead(fRefNum,(long*)dataLen,data);
- if (err==noErr) {
- testGet = 1;
- err = FSRead(fRefNum,&testGet,testData);
- if (err==noErr)
- err = kMoreData;
- }
-
- if (err==eofErr)
- err = noErr;
- if (err!=noErr) {
- FSClose(fRefNum);
- return err;
- }
-
- err = FSClose(fRefNum);
-
- return err;
- }
-
-
- /* get the length of a spool element in bytes */
-
- OSErr GetSpoolLength(FSSpec *sFile,OSType type,OSType creator,long id,unsigned long *dataLen)
- {
- OSErr err;
- Str255 idStr;
- long dirID;
- char osTypeStr[5];
- CInfoPBRec infoRec;
- Str31 fName;
-
- NumToString(id,idStr);
-
- /* get dirID of parent */
-
- err = GetFSSpecDirID(sFile,&dirID);
- if (err!=noErr) {
- return err;
- }
-
- pstrcpy(fName,"\pdata");
- pstrcat(fName,idStr);
- osTypeStr[0] = 4;
- BlockMove(&creator,&osTypeStr[1],4);
- pstrcat(fName,osTypeStr);
- BlockMove(&type,&osTypeStr[1],4);
- pstrcat(fName,osTypeStr);
-
- infoRec.hFileInfo.ioNamePtr = fName;
- infoRec.hFileInfo.ioVRefNum = sFile->vRefNum;
- infoRec.hFileInfo.ioFDirIndex = 0;
- infoRec.hFileInfo.ioDirID = dirID;
- err = PBGetCatInfo(&infoRec,false);
- *dataLen = infoRec.hFileInfo.ioFlLgLen;
- if (err!=noErr)
- err = kNoData;
- return err;
- }
- /*---------------------*/
-
- /* recursively delete a sub-tree */
-
- OSErr RecursiveDeleteDirectory(short vRefNum,long dirID,StringPtr fName)
- {
- OSErr err;
- CInfoPBRec pBlock;
- HParamBlockRec ourDelBlock;
- Str255 ourNameStore;
-
- /* save our location for tail delete reference */
-
- BlockMove(fName,ourNameStore,sizeof(Str255));
- ourDelBlock.ioParam.ioNamePtr = ourNameStore;
- ourDelBlock.ioParam.ioVRefNum = vRefNum;
- ourDelBlock.fileParam.ioDirID = dirID;
-
- /* delete children */
-
- pBlock.dirInfo.ioNamePtr = fName;
- pBlock.dirInfo.ioVRefNum = vRefNum;
- pBlock.hFileInfo.ioFVersNum = 0;
- pBlock.dirInfo.ioFDirIndex = 0;
- pBlock.dirInfo.ioDrDirID = dirID;
- err = PBGetCatInfo(&pBlock,false);
- if (err!=noErr)
- return err;
- dirID = pBlock.dirInfo.ioDrDirID;
-
- do {
- pBlock.dirInfo.ioDrDirID = dirID;
- pBlock.dirInfo.ioFDirIndex = 1;
- err = PBGetCatInfo(&pBlock,false);
- if (err==noErr) {
- if ((pBlock.dirInfo.ioFlAttrib & 0x10) != 0) // directory
- RecursiveDeleteDirectory(vRefNum,pBlock.dirInfo.ioDrDirID,fName);
- else {
- pBlock.hFileInfo.ioDirID = pBlock.hFileInfo.ioFlParID;
- err = PBHDelete((HParmBlkPtr)&pBlock,false);
- }
- }
- } while (err==noErr);
-
- /* delete ourselves */
-
- pBlock.dirInfo.ioNamePtr = fName;
- pBlock.dirInfo.ioDrDirID = dirID;
- pBlock.dirInfo.ioFDirIndex = -1;
- pBlock.hFileInfo.ioFVersNum = 0;
- err = PBGetCatInfo(&pBlock,false);
- if (err==noErr) {
- ourDelBlock.fileParam.ioDirID = pBlock.dirInfo.ioDrParID;
- err = PBHDelete(&ourDelBlock,false);
- }
- return err;
- }
-
-
- /* get a directory's directory ID from it's FSSpec */
-
- OSErr GetFSSpecDirID(FSSpec *fSpec,long *dirID)
- {
- OSErr err;
- CInfoPBRec pBlock;
-
- pBlock.dirInfo.ioNamePtr = fSpec->name;
- pBlock.dirInfo.ioVRefNum = fSpec->vRefNum;
- pBlock.dirInfo.ioDrDirID = fSpec->parID;
- pBlock.dirInfo.ioFDirIndex = 0;
- err = PBGetCatInfo(&pBlock,false);
- *dirID = pBlock.dirInfo.ioDrDirID;
- return err;
- }
-